package com.dunke.web.controller;

import com.dunke.core.common.Result;
import com.dunke.core.data.GetData;
import com.dunke.core.data.IpAddrData;
import com.dunke.core.data.PageData;
import com.dunke.core.data.admin.AdminInfo;
import com.dunke.core.data.admin.AdminPassword;
import com.dunke.core.data.admin.AdminLogin;
import com.dunke.core.data.admin.AdminRegister;
import com.dunke.service.AdminService;
import com.dunke.utils.IpServletUtils;
import com.dunke.utils.JedisUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (DbAdmin)表控制层
 * 管理员
 * @author makejava
 * @since 2021-06-23 18:37:25
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;

    /**
     * 用户登录
     * @param login 实例对象
     * @return 是否成功
     */
    @RequestMapping(path="/login", method={RequestMethod.POST,
            RequestMethod.GET})
    public Result login(@Validated @RequestBody AdminLogin login,
                        HttpServletRequest request) {
        IpServletUtils.getIp(request);
        return this.adminService.login(login);
    }

    /**
     * 注册账户
     * @param register 实例对象
     * @return 是否成功
     */
    @RequestMapping(path="/register", method=RequestMethod.PUT)
    public Result register(@Validated @RequestBody AdminRegister register) {
        return this.adminService.insert(register);
    }

    /**
     * 忘记密码
     * @param password 实例对象
     * @return 是否成功
     */
    @RequestMapping(path="/forgotPwd", method=RequestMethod.POST)
    public Result forgotPwd(@Validated @RequestBody AdminPassword password) {
        return this.adminService.updatePwd(password);
    }

    /**
     * 修改密码
     * @param password 实例对象
     * @return 是否成功
     */
    @RequestMapping(path="/modifyPwd", method=RequestMethod.POST)
    public Result modifyPwd(@Validated @RequestBody AdminPassword password) {
        return this.adminService.updatePwd(password);
    }

    /**
     * 查询指定用户
     * @param request 头部参数
     * @return 单条数据
     */
    @RequestMapping(path="/getInfo", method={RequestMethod.POST,
            RequestMethod.GET})
    public Result getInfo(HttpServletRequest request) {
        JedisUtil jedis = new JedisUtil();
        int id = jedis.getTokenId(request.getHeader("token"));
        return this.adminService.queryById(id);
    }

    /**
     * 修改用户信息
     * @param request
     * @return
     */
    @RequestMapping(path="/update", method=RequestMethod.POST)
    public Result update(@Validated @RequestBody AdminInfo info,
                             HttpServletRequest request) {
        JedisUtil jedis = new JedisUtil();
        int id = jedis.getTokenId(request.getHeader("token"));
        info.setId(id);
        info.setAccount("");
        return this.adminService.update(info);
    }

    /**
     * 查询多条数据
     * @param paged 查询页数、每页条数
     * @return
     */
    @RequestMapping(path="/queryAll", method={RequestMethod.POST,
            RequestMethod.GET})
    public Result queryAll(@Validated @RequestBody PageData paged) {
        return this.adminService.queryAllByLimit(paged);
    }

    /**
     * 通过主键删除数据
     * @param data 主键
     * @return 是否成功
     */
    @RequestMapping(path="/del", method={RequestMethod.POST,
            RequestMethod.GET})
    public Result delById(@Validated @RequestBody GetData data) {
        /**
         * 一般不允许删除
         */
        return this.adminService.deleteById(data);
    }

    /**
     * 退出登录
     * @param request 头部参数
     * @return 单条数据
     */
    @RequestMapping(path="/loginOut", method={RequestMethod.POST,
            RequestMethod.GET})
    public Result loginOut(HttpServletRequest request) {
        return this.adminService.loginOut(request.getHeader("token"));
    }

    /**
     * 新增登录日志
     * @param paged 分页记录
     * @param request 头部参数
     * @return
     */
    @RequestMapping(path="/addLog", method={RequestMethod.POST,
            RequestMethod.GET})
    public Result addLog(@Validated @RequestBody IpAddrData paged,
                         HttpServletRequest request) {
        JedisUtil jedis = new JedisUtil();
        paged.setAid(jedis.getTokenId(request.getHeader("token")));
        paged.setIp(IpServletUtils.getIp(request));
        return this.adminService.insertLog(paged);
    }

    /**
     * 获取登录日志
     * @param paged 分页记录
     * @param request 头部参数
     * @return
     */
    @RequestMapping(path="/getLog", method={RequestMethod.POST,
            RequestMethod.GET})
    public Result getLog(@Validated @RequestBody PageData paged,
                         HttpServletRequest request) {
        JedisUtil jedis = new JedisUtil();
        int id = jedis.getTokenId(request.getHeader("token"));
        return this.adminService.queryLogById(paged, id);
    }

    /**
     * 获取登录日志
     * @param data 指定日志id
     * @param request 头部参数
     * @return
     */
    @RequestMapping(path="/delLog", method=RequestMethod.POST)
    public Result delLog(@Validated @RequestBody GetData data,
                         HttpServletRequest request) {
        JedisUtil jedis = new JedisUtil();
        int aid = jedis.getTokenId(request.getHeader("token"));
        return this.adminService.delLog(data, aid);
    }
}
